package com.ykt.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.ykt.interceptor.Seach;

/**
 * 
 * @author Tianjun 
 *
 */
public class Product extends SeachModel<Product>{
	private static final long serialVersionUID = 1L;
	public static final Product dao = new Product();
	
	//得到类型
	public ProductType getProductType(){
		return ProductType.dao.findById(this.getInt("product_type_id"));
	}
	
	//得到图片
	public List<ProductImg> getProductImgs(){
		String sql="select * from product_img where product_id=?";
		return ProductImg.dao.find(sql, this.getInt("id"));
	}
	
	//删除所有图片
	public void deleteAllPic(Integer product_id){
		if(product_id!=null)
			Db.update("delete from product_img where product_id=?", product_id);
	}
	
	public Page<Product> paginate(int pageNumber,int rows,Seach seach){
		String select="select product.*,product_type.name as product_type_name";
		String sqlExceptSelect = " from product left join product_type on product.product_type_id = product_type.id "
								+ "left join shop s on product_type.shop_id = s.id  where 1=1 ";
		return paginateBySelect(select, sqlExceptSelect, pageNumber, rows, seach);
	}

}
